package cn.hls.web.controller.biz;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.hls.entity.Member;
import cn.hls.entity.vo.Msg;
import cn.hls.entity.vo.PageInfo;
import cn.hls.service.MemberService;
import cn.hls.web.controller.base.BaseController;
import cn.hls.web.task.JobService;

/**
 * @fileName: MemberController.java
 * @package: cn.hls.web.controller.biz
 * @Description: 人员信息控制
 * @author: yinghui.zhang
 * @email: uuhui@163.com
 * @date: 2015-7-18
 */
@Controller
@RequestMapping("/member")
public class MemberController extends BaseController<Member> {
	 
	@Resource(name="memberService")
	private MemberService memberService;
		/**
		 * 表单查看或修改
		 * @param id
		 * @param show
		 * @param print 是否打印
		 * @return
		 * @throws Exception
		 */
		@RequestMapping(value = "/info")
		public ModelAndView info(Long id, boolean show,boolean print )
				throws Exception {
			Member entity = null;
			if (id != null) {
				entity = memberService.find(id);
			}
			ModelAndView modelAndView=new ModelAndView();
			modelAndView.addObject("entity",entity);
			if (show) {
				modelAndView.setViewName("/member/show");// 查看页面
			} else {
				if(print){
					modelAndView.setViewName("member/print");//打印页面
				}else{
					modelAndView.setViewName("member/info");// 新增、修改页面
				}
			}
			return modelAndView;
		}

		/**
		 * 添加操作
		 * 
		 * @param entity
		 * @return
		 * @throws Exception
		 */
		@RequestMapping("/add")
		@ResponseBody
		public Msg add(Member entity) throws Exception {
			if(!memberService.exists(entity)){
				if(entity.getCompany()!=null){
					if(entity.getCompany().getId()==null){
						entity.setCompany(null);
					}
				}
				boolean flag = memberService.add(entity);
				if (flag) {
					msg.setInfo("操作成功");
					msg.setStatus(200);
				} else {
					msg.setInfo("操作失败");
					msg.setStatus(201);
				}
			}else{
				msg.setInfo("存在相同的人员信息!");
				msg.setStatus(201);
			}
			return msg;
		}

		 
		/**
		 * 删除操作
		 * 
		 * @param id
		 * @return
		 * @throws Exception
		 */
		@ResponseBody
		@RequestMapping("/del")
		public Msg del(String ids) throws Exception {
			String[] tempIds = ids.split("\\,");
			memberService.delete(tempIds);
			return msg;
		}

		/**
		 * 修改操作
		 * @param entity
		 * @return
		 * @throws Exception
		 */
		@RequestMapping("/update")
		@ResponseBody
		public Msg update(Member entity) throws Exception {
			try {
				boolean flag = memberService.update(entity);
				if (flag) {
					msg.setInfo("操作成功");
					msg.setStatus(200);
				} else {
					msg.setInfo("操作失败");
					msg.setStatus(201);
				}
			} catch (Exception e) {
				e.printStackTrace();
				msg.setInfo(e.getMessage());

			}
			return msg;
		}

		/**
		 * 分页查询
		 * 
		 * @param entity 查询的会友对象
		 * @param page 会友分页对象
		 * @param nodeIsLeaf 判断选择的节点是否分叶子节点
		 * @return PageInfo<member>
		 * @throws Exception
		 */
		@RequestMapping("/list")
		@ResponseBody
		public PageInfo<Member> list(Member entity, PageInfo<Member> page)
				throws Exception {
			return memberService.loadMemberInfo(entity, page);
		}
		
		@RequestMapping("/loadMemberList")
		@ResponseBody
		public List<Member> loadMemmberList(Member entity)throws Exception{
			return memberService.loadMemberList(entity);
		}
	
		@RequestMapping("/view")
		public ModelAndView view(HttpServletRequest request) {
			return new ModelAndView("/member/list").addObject("pa",request.getParameter("pa"));

		}

		@Resource(name="jobService")
		JobService job;
		@ResponseBody
		@RequestMapping("/test")
		public String msg()throws Exception{
			job.sendMsgOfExpired();
			return "success";
		}
}
